<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2018/3/28
 * Time: 13:51
 */

namespace app\admin\controller\log;


use app\common\controller\Backend;
use app\common\utils\Constant;

/**
 * 资金流动记录
 * Class MoneyLog
 * @package app\admin\controller\log
 */
class MoneyLog extends Backend
{
    protected $model = null;

    public function _initialize(){
        parent::_initialize();
        $this->model = model('MoneyLog');
    }

    /**
     * 查看
     */
    public function index()
    {
        if ($this->request->isAjax())
        {
            $userId = $this->auth->id;
            $user = model('User')->where(array('id'=>$userId))->field('user_type,property_id')->find();
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $map = [];
            if($user['user_type'] == Constant::USER_TYPE_PROPERTY) {
                $map['property_id'] = $user['property_id'];
            }
            $total = $this->model
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->where($map)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $key=>&$value){
                $value['user_name'] = model('User')->where(array('id'=>$value['user_id']))->value('nickname');
                $value['house_property_name'] = model('HouseProperty')->where(array('id'=>$value['house_property_id']))->value('detail');
            }
            unset($value);
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
}